package top.myhdg.bijou.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;

import com.gyf.barlibrary.ImmersionBar;
import com.youngfeng.snake.annotations.EnableDragToClose;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import top.myhdg.bijou.R;
import top.myhdg.bijou.colorUi.util.SharedPreferencesMgr;

@EnableDragToClose()
public class BaseActivity extends AppCompatActivity {

    private ImmersionBar immersionBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_USER);//允许旋转屏幕

        if (SharedPreferencesMgr.getInt("Theme", 0) == 1) {
            setTheme(R.style.NightTheme);
        } else {
            setTheme(R.style.DayTheme);
        }

        if (!ImmersionBar.isSupportStatusBarDarkFont()) {
            immersionBar = ImmersionBar.with(this)
                    .statusBarColor(R.color.Black);
        } else {
            MIUISetStatusBarLightMode(this);

            immersionBar = ImmersionBar.with(this)
                    .transparentBar() //透明导航栏和状态栏
//                    .navigationBarColor(R.color.White)//导航栏颜色
                    .flymeOSStatusBarFontColor(R.color.Black)//flyme状态栏字体颜色
                    .statusBarDarkFont(true);//状态栏黑字
        }

        immersionBar.init();   //所有子类都将继承这些相同的属性
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (immersionBar != null)
            immersionBar.destroy();
    }


    private static void MIUISetStatusBarLightMode(Activity activity) {
        Window window = activity.getWindow();
        if (window != null) {
            try {
                int darkModeFlag;
                @SuppressLint("PrivateApi") Class layoutParams = Class.forName("android.view.MiuiWindowManager$LayoutParams");
                Field field = layoutParams.getField("EXTRA_FLAG_STATUS_BAR_DARK_MODE");
                darkModeFlag = field.getInt(layoutParams);
                Method extraFlagField = window.getClass().getMethod("setExtraFlags", int.class, int.class);
                extraFlagField.invoke(window, darkModeFlag, darkModeFlag);//状态栏透明且黑色字体

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    //开发版 7.7.13 及以后版本采用了系统API，旧方法无效但不会报错，所以两个方式都要加上
                    activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}
